package com.huiqiao.bus.controller;

import com.huiqiao.bus.service.ICustomerService;
import com.huiqiao.bus.service.IRentService;
import com.huiqiao.bus.vo.RentVo;
import com.huiqiao.sys.constant.SysConstant;
import com.huiqiao.sys.domain.User;
import com.huiqiao.sys.utils.DataGridView;
import com.huiqiao.sys.utils.RandomUtils;
import com.huiqiao.sys.utils.ResultObj;
import com.huiqiao.sys.utils.WebUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.Objects;

/**
 * 汽车出租管理的控制器
 * @Author: yanliwei@foolish
 * @CreateTime: 2024-12-18  09:38
 */
@RestController
@RequestMapping("rent")
public class RentController {

    @Autowired
    private ICustomerService customerService;

    @Autowired
    private IRentService rentService;



    /**
     *检查身份证号是否存在
     * @param rentVo
     * @return
     */
    @RequestMapping("checkCustomerExist")
    public ResultObj checkCustomerExist(RentVo rentVo){
        if (Objects.nonNull(customerService.checkCustomerExist(rentVo.getIdentity()))){
            return ResultObj.STATUS_TRUE;
        }else {
            return ResultObj.STATUS_FALSE;
        }
    }

    /**
     * 初始化添加出租单的表单的数据
     * @param rentVo
     * @return
     */
    @RequestMapping("initRentFrom")
    public RentVo initRentFrom(RentVo rentVo){
        //已经出租的汽车不能再次出租
        try {

        }catch (Exception e){
            e.printStackTrace();

        }
        //⽣成出租单号
        rentVo.setRentid(RandomUtils.createRandomStringUseTime(SysConstant.CAR_ORDER_CZ));
        //设置起租时间
        rentVo.setStartTime(new Date());
        //设置操作员
        User user = (User) WebUtils.getHttpSession().getAttribute("user");
        if(Objects.nonNull(user)){
            rentVo.setOpername(user.getRealname());
        }else {
            rentVo.setOpername("系统管理员");
        }
        return rentVo;
    }

    @RequestMapping("loadAllRent")
    public DataGridView loadAllRent(RentVo rentVo){
        return this.rentService.queryAllRent(rentVo);
    }


}
